//
//  CPuberInfoView.m
//  HCHK_iphone
//
//  Created by yilong zhang on 12-1-4.
//  Copyright 2012年 __MyCompanyName__. All rights reserved.
//

#import "CPuberInfoView.h"

@implementation CPuberInfoView

@synthesize m_Icon;
@synthesize m_Nick;

@synthesize m_MyTitle;
@synthesize m_FrdTitle;
@synthesize m_FrdNum;
@synthesize m_PrdTitle;
@synthesize m_PrdNum;
@synthesize m_ComTitle;
@synthesize m_ComNum;
@synthesize m_Gallary;
@synthesize m_NoFavos;
@synthesize m_Datas;
@synthesize m_PubID;
@synthesize m_NetCnt;
@synthesize m_NativeCnt;
@synthesize m_bIsLoading;
@synthesize m_Indicator;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // Initialization code here.
        self.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1];
        self.m_Title = @"发布人资料";
        m_bIsLoading = NO;
        
        m_Datas = [[NSMutableArray array] retain];
        m_NetCnt= [[CAscCounter alloc] init];
        m_NetCnt.m_nLimit = 6;
        m_NetCnt.m_nOffset= 0;
        
        m_NativeCnt = [[CAscCounter alloc] init];
        m_NativeCnt.m_nLimit = 6;
        m_NativeCnt.m_nOffset= 0;
        
        m_Icon = [[UIImageView alloc] initWithFrame:CGRectMake(20, 40, 65, 65)];
        m_Icon.image = [UIImage imageNamed:@"icon.png"];
        [self addSubview:m_Icon];
        [m_Icon release];
        
        UIFont *font = [UIFont fontWithName:@"Helvetica" size:17];
        m_Nick = [[UILabel alloc] initWithFrame:CGRectMake(90, 80, 150, 25)];
        m_Nick.backgroundColor = [UIColor clearColor];
        m_Nick.text = @"昵称";
        m_Nick.font = font;
        [self addSubview:m_Nick];
        [m_Nick release];
        
        UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(0, 125, frame.size.width, 1)];
        line.image = [UIImage imageNamed:@"line.png"];
        [self addSubview:line];
        [line release];
        
        font = [UIFont fontWithName:@"Helvetica" size:14];
        
        m_FrdTitle = [[UILabel alloc] initWithFrame:CGRectMake(40, 130, 79, 25)];
        m_FrdTitle.text = @"发布人好友";
        m_FrdTitle.backgroundColor = [UIColor clearColor];
        m_FrdTitle.textAlignment = UITextAlignmentCenter;
        m_FrdTitle.font = font;
        [self addSubview:m_FrdTitle];
        [m_FrdTitle release];
        
        m_FrdNum = [[UILabel alloc] initWithFrame:CGRectMake(40, 155, 79, 25)];
        m_FrdNum.text = @"(0)";
        m_FrdNum.backgroundColor = [UIColor clearColor];
        m_FrdNum.textAlignment = UITextAlignmentCenter;
        m_FrdNum.font = font;
        [self addSubview:m_FrdNum];
        [m_FrdNum release];
        
        line = [[UIImageView alloc] initWithFrame:CGRectMake(120, 130, 1, 50)];
        line.image = [UIImage imageNamed:@"vline.png"];
        [self addSubview:line];
        [line release];
        
        m_PrdTitle = [[UILabel alloc] initWithFrame:CGRectMake(121, 130, 78, 25)];
        m_PrdTitle.text = @"发布人美食";
        m_PrdTitle.backgroundColor = [UIColor clearColor];
        m_PrdTitle.textAlignment = UITextAlignmentCenter;
        m_PrdTitle.font = font;
        [self addSubview:m_PrdTitle];
        [m_PrdTitle release];
        
        m_PrdNum = [[UILabel alloc] initWithFrame:CGRectMake(121, 155, 78, 25)];
        m_PrdNum.text = @"(0)";
        m_PrdNum.backgroundColor = [UIColor clearColor];
        m_PrdNum.textAlignment = UITextAlignmentCenter;
        m_PrdNum.font = font;
        [self addSubview:m_PrdNum];
        [m_PrdNum release];
        
        line = [[UIImageView alloc] initWithFrame:CGRectMake(200, 130, 1, 50)];
        line.image = [UIImage imageNamed:@"vline.png"];
        [self addSubview:line];
        [line release];
        
        m_ComTitle = [[UILabel alloc] initWithFrame:CGRectMake(201, 130, 79, 25)];
        m_ComTitle.text = @"发布人评价";
        m_ComTitle.backgroundColor = [UIColor clearColor];
        m_ComTitle.textAlignment = UITextAlignmentCenter;
        m_ComTitle.font = font;
        [self addSubview:m_ComTitle];
        [m_ComTitle release];
        
        m_ComNum = [[UILabel alloc] initWithFrame:CGRectMake(201, 155, 79, 25)];
        m_ComNum.text = @"(0)";
        m_ComNum.backgroundColor = [UIColor clearColor];
        m_ComNum.textAlignment = UITextAlignmentCenter;
        m_ComNum.font = font;
        [self addSubview:m_ComNum];
        [m_ComNum release];
        
        line = [[UIImageView alloc] initWithFrame:CGRectMake(0, 185, frame.size.width, 1)];
        line.image = [UIImage imageNamed:@"line.png"];
        [self addSubview:line];
        [line release];
        
        font = [UIFont fontWithName:@"Helvetica" size:15];
        
        m_MyTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 205, frame.size.width, 25)];
        m_MyTitle.text = @"发布人美食";
        m_MyTitle.backgroundColor = [UIColor clearColor];
        m_MyTitle.textAlignment = UITextAlignmentCenter;
        m_MyTitle.font = font;
        [self addSubview:m_MyTitle];
        [m_MyTitle release];
        
        line = [[UIImageView alloc] initWithFrame:CGRectMake(0, 235, frame.size.width, 1)];
        line.image = [UIImage imageNamed:@"line.png"];
        [self addSubview:line];
        [line release];
        
        m_Gallary = [[CGalleryView alloc] initWithFrame:CGRectMake(0, 236, frame.size.width, 64) ItemSize:CGSizeMake(64, 64) Horizontal:YES];
        m_Gallary.m_delegate = self;
        [self addSubview:m_Gallary];
        [m_Gallary release];
        
        m_Indicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(m_Gallary.frame.size.width-30, m_Gallary.frame.origin.y+19, 26, 26)];
        m_Indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
        m_Indicator.hidesWhenStopped = YES;
        [self addSubview:m_Indicator];
        [m_Indicator release];
        
        m_NoFavos = [[UILabel alloc] initWithFrame:CGRectMake(0, 236, frame.size.width, 64)];
        m_NoFavos.text = @"数据加载中";//@"对不起，还没有发布过任何美食。";
        m_NoFavos.textColor = [UIColor redColor];
        m_NoFavos.backgroundColor = [UIColor clearColor];
        m_NoFavos.textAlignment = UITextAlignmentCenter;
        m_NoFavos.font = m_FrdNum.font;
        [self addSubview:m_NoFavos];
        [m_NoFavos release];
        
        line = [[UIImageView alloc] initWithFrame:CGRectMake(0, 300, frame.size.width, 1)];
        line.image = [UIImage imageNamed:@"line.png"];
        [self addSubview:line];
        [line release];
    }
    
    return self;
}

-(void)dealloc
{
    [m_Datas removeAllObjects];
    [m_Datas release];
    
    [m_NativeCnt release];
    [m_NetCnt release];
    
    [super dealloc];
}

-(void)ShowIndicator
{
    [m_Gallary ShowIndicator];
    return;
    
    if (!m_Indicator.isAnimating)
    {
        [UIView beginAnimations:nil context:nil];
        m_Gallary.center = CGPointMake(m_Gallary.center.x-m_Indicator.frame.size.width, m_Gallary.center.y);
        [m_Indicator startAnimating];
        [UIView commitAnimations];
    }
}
-(void)HiddenIndicator
{
    [m_Gallary HiddenIndicator];
    return;
    
    if (m_Indicator.isAnimating)
    {
        [UIView beginAnimations:nil context:nil];
        m_Gallary.center = CGPointMake(m_Gallary.center.x+m_Indicator.frame.size.width, m_Gallary.center.y);
        [m_Indicator stopAnimating];
        [UIView commitAnimations];
    }
}

-(void)GetFriendUpload
{
    /*NSArray *results = [self GetMyUploadFromNative];
    
    //本地没有数据或者已经读完
    if (!results)
    {
        m_NativeCnt.m_nOffset = 0;
        //[self CommitGetPubInfoRequest:m_NetCnt.m_nOffset Limit:m_NetCnt.m_nLimit Action:OP_GETUPLOAD_PRODUCT PubID:m_PubID];
    }
    else 
    {
        [self AddProductsWithArray:results];
        m_NativeCnt.m_nOffset += 6;
        m_bIsLoading = NO;
        [self HiddenIndicator];
    }*/
    
    [self CommitGetPubInfoRequest:m_NetCnt.m_nOffset Limit:m_NetCnt.m_nLimit Action:OP_GETUPLOAD_PRODUCT PubID:m_PubID];
}

-(void)DidGalleryDragEnd:(id)object
{
    if (!m_bIsLoading)
    {
        [self ShowIndicator];
    }
}

-(void)DidGalleryBottom:(id)object
{
    if (!m_bIsLoading)
    {
        m_bIsLoading = YES;
        //hchk-test-start
        if (0 == m_NetCnt.m_nLimit)
        {
            m_bIsLoading = NO;
            [self HiddenIndicator];
            return;
        }
        //hchk-test-end
        [self GetFriendUpload];
    }
}

-(void)DidGallerySelected:(int)index Directory:(int)directory Object:(id)object
{
    return;
}

-(void)DidGalleryTurnPage:(int)index Directory:(int)directory Object:(id)object
{
    CProduct *product = nil;
    CGalleyItem *item = nil;
    NSArray *subs = [m_Gallary.m_RootPanel subviews];
    NSData *data = nil;
    UIImage*image= nil;
    int start = 0;
    int cnt   = 0;
    int total = [[m_Gallary.m_RootPanel subviews] count] - 1;//[m_Datas count]-1;
    
    //left + 
    if (0 == directory)
    {
        //释放图片
        cnt   = (index >= 5) ? (5) : (index);
        start = index - 1;
        for (int i=0; i<cnt; i++)
        {
            //NSLog(@"释放图片");
            if (start < [m_Datas count])
            {
                product = [m_Datas objectAtIndex:start];
                [product ProductCancelLoadIcon];
                item = [subs objectAtIndex:start--];
                item.m_ImageView.image = nil;
            }
        }
        //加载图片
        cnt = (index+6 <= total) ? (6) : (total - index);
        start = index;
        (start+cnt <= total) ? (++cnt) : (cnt);
        for (int i=0; i<cnt; i++)
        {
            //NSLog(@"加载图片");
            if (start < [m_Datas count])
            {
                product = [m_Datas objectAtIndex:start];
                item = [subs objectAtIndex:start++];
                if (!item.m_ImageView.image)
                {
                    data = [product GetProductIcon];
                    if (data)
                    {
                        image = [UIImage imageWithData:data];
                    }
                    item.m_ImageView.image = image;
                    image = nil;
                }
            }
        }
    }
    //right <-
    else if (1 == directory)
    {
        //加载图片
        cnt   = (index >= 5) ? (5) : (index);
        (0 == index) ? (start=index, cnt=1) : (start=index - 1);
        for (int i=0; i<cnt; i++)
        {
            //NSLog(@"加载图片");
            if (start < [m_Datas count])
            {
                product = [m_Datas objectAtIndex:start];
                item = [subs objectAtIndex:start--];
                if (!item.m_ImageView.image)
                {
                    data = [product GetProductIcon];
                    if (data)
                    {
                        image = [UIImage imageWithData:data];
                    }
                    item.m_ImageView.image = image;
                    image = nil;
                }
            }
        }
        //释放图片
        cnt = (index+12 <= total) ? (6) : (total - (index+6));
        start = index+6;
        for (int i=0; i<cnt; i++)
        {
            //NSLog(@"释放图片");
            if (start < [m_Datas count])
            {
                product = [m_Datas objectAtIndex:start];
                [product ProductCancelLoadIcon];
                item = [subs objectAtIndex:start++];
                item.m_ImageView.image = nil;
            }
        }
    }
    else {;}
}

-(void)SetPuberInfoView:(CProduct*)product
{
    //m_Nick.text = product.P_PubName;
}

-(void)AddProductsWithArray:(NSArray*)array
{
    CProduct *product = nil;
    CGalleyItem *item = nil;
    NSMutableArray *items = [NSMutableArray array];
    NSData *data = nil;
    UIImage*image= nil;
    for (int i=0; i<[array count]; i++)
    {
        product = (CProduct*)[array objectAtIndex:i];
        data  = [product GetProductIcon];
        if (data)
        {
            image = [UIImage imageWithData:data];
        }
        else 
        {
            image = [CMainPageCtrl DefaultMainPageCtrl].m_DfIcon;
            [product DownLoadIcon];
        }
        item = [[CGalleyItem alloc] initWithImage:image];
        product.m_IconView = item.m_ImageView;
        [items addObject:item];
        [m_Datas addObject:product];
        //[product release];
        [item release];
    }
    
    [m_Gallary SetItems:items Item:nil];
}

-(void)AddProducts:(NSDictionary*)dic
{
    CProduct *product = nil;
    CGalleyItem *item = nil;
    NSData *data = nil;
    UIImage*image= nil;
    NSArray *items = [dic objectForKey:@"Article"];
    NSMutableArray *arr = [NSMutableArray array];
    if ([items isKindOfClass:[NSArray class]])
    {
        for (NSDictionary *sub in items)
        {
            product = [[CProduct alloc] init];
            [product SetProductWithDic:sub Path:[AppConfig DefaultAppConfig].m_DelImagesPath];
            data  = [product GetProductIcon];
            if (data)
            {
                image = [UIImage imageWithData:data];
            }
            else 
            {
                [product DownLoadIcon];
            }
            item = [[CGalleyItem alloc] initWithImage:image];
            product.m_IconView = item.m_ImageView;
            [arr addObject:item];
            [m_Datas addObject:product];
            [product release];
            [item release];
        }

        [m_Gallary SetItems:arr Item:nil];
        
        [m_NetCnt GetAscNextLimit:m_NetCnt.m_nTotal Cnt:[items count]];
    }
}

-(NSArray*)GetMyUploadFromNative
{
    NSArray *rets = nil;
    
    if (m_NativeCnt.m_nLimit > 0)
    {
        NSString *tablename = [NSString stringWithFormat:@"%@_upload", [CUser DefaultUser].U_Name];
        rets = [CProduct GetLimitProducts:tablename Where:nil Offset:m_NativeCnt.m_nOffset Limit:m_NativeCnt.m_nLimit]; 
    }
    
    return rets;
}

-(void)CommitGetPubInfoRequest:(int)offset Limit:(int)limit Action:(NSString*)action PubID:(NSNumber*)pubid
{
    NSMutableString *request = [NSMutableString string];
    
    [request appendFormat:@"cursor=%d", offset];
    [request appendFormat:@"&offset=%d", limit];
    [request appendFormat:@"&action=%@", action];
    
    [request appendFormat:@"&clientID=%@", [AppConfig DefaultAppConfig].m_strUDID];
    [request appendFormat:@"&mobikey=%@", [CUser DefaultUser].U_Token];
    [request appendFormat:@"&sessionid=%@", [CUser DefaultUser].U_Session];
    [request appendFormat:@"&fid=%lld", [m_PubID longLongValue]];
    
    NSLog(@"\r\n\r\n获得好友信息:%@\r\n\r\n", request);
    
    [[CDownLoadManager DefaultDownLoadManager] AddTask:URL_GETMYUPLOAD Sponsor:self PostData:[request dataUsingEncoding:NSUTF8StringEncoding] TimeOut:30 Data:pubid MType:Post UserInfo:nil];
}

-(void)GetPubInfoFromSrv:(CProduct*)product
{
    if (m_PubID != product.P_Publisher)
    {
        [[CRootCtrlManager DefaultRootCtrl] ShowLoadView:0.8 Title:@"数据获取中..."];
        
        m_PubID = product.P_Publisher;
        m_Nick.text = product.P_PubName;
        [m_Datas removeAllObjects];
        [m_Gallary ResetGallery];
        
        [self CommitGetPubInfoRequest:m_NetCnt.m_nOffset Limit:m_NetCnt.m_nLimit Action:OP_GETUPLOAD_PRODUCT PubID:m_PubID];
    }
}

-(void)DidGetPubInfo:(NSDictionary*)dic
{
    if (0 == [m_Datas count])
    {
        self.m_FrdNum.text = [NSString stringWithFormat:@"(%@)", [dic objectForKey:@"CountFriend"]];
        self.m_PrdNum.text = [NSString stringWithFormat:@"(%@)", [dic objectForKey:@"CountArticle"]];
        self.m_ComNum.text = [NSString stringWithFormat:@"(%@)", [dic objectForKey:@"CountComment"]];
        
        m_NoFavos.text = nil;
    }
    
    [self AddProducts:dic];
}

//operation delegate
-(void)DidOperationFinished:(COperation *)op Data:(NSData *)data Other:(id)other UserInfo:(id)userinfo
{
    if (m_PubID == other)
    {
        NSLog(@"\r\n\r\n<====Publisher====>\r\n\r\n");
        
        NSString     *json = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSDictionary *dic  = [json JSONValue];
        NSLog(@"\r\n\r\n收到美食发布者的原始数据:%@\r\n\r\n",json);
        
        if (dic)
        {
            if (0 == [[dic objectForKey:@"Msg_error"] intValue])
            {
                NSDictionary *subdic = [dic objectForKey:@"Resource"];
                
                m_NetCnt.m_nTotal = [[dic objectForKey:@"CountFriend"] intValue];
                [self DidGetPubInfo:subdic];
            }
            else 
            {
                UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:[dic objectForKey:@"Msg"] delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil];
                [alter show];
                [alter autorelease];
            }
        }
        else 
        {
            NSLog(@"Error:%@", json);
            //m_NoFavos.text = @"获取信息失败";
        }
        
        [json release];
        
        m_bIsLoading = NO;
    }
    
    [self HiddenIndicator];
    [[CRootCtrlManager DefaultRootCtrl] HiddenLoadView];
}

-(void)DidOperationError:(COperation *)op Error:(NSError *)error Other:(id)other UserInfo:(id)userinfo
{
    m_bIsLoading = NO;
    //m_NoFavos.text = @"获取信息失败";
    [self HiddenIndicator];
    [[CRootCtrlManager DefaultRootCtrl] HiddenLoadView];
}

-(void)DidOperationTimeOut:(COperation *)op Other:(id)other UserInfo:(id)userinfo
{
    m_bIsLoading = NO;
    //m_NoFavos.text = @"获取信息失败";
    [self HiddenIndicator];
    [[CRootCtrlManager DefaultRootCtrl] HiddenLoadView];
}

//product delegate

@end
